/*    */ package xin.petstore.framework.cipher;
/*    */ 
/*    */ import java.security.Key;
/*    */ import java.security.SecureRandom;
/*    */ import javax.crypto.Cipher;
/*    */ import javax.crypto.KeyGenerator;
/*    */ import sun.misc.BASE64Decoder;
/*    */ import sun.misc.BASE64Encoder;
/*    */ 
/*    */ public class TextDESEncryptor
/*    */   implements Encryptor
/*    */ {
/*    */   private Key key;
/*    */ 
/*    */   public void setPassword(String password)
/*    */   {
/*    */     try
/*    */     {
/* 21 */       KeyGenerator generator = KeyGenerator.getInstance("DES");
/* 22 */       SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
/* 23 */       secureRandom.setSeed(password.getBytes());
/* 24 */       generator.init(secureRandom);
/* 25 */       this.key = generator.generateKey();
/* 26 */       generator = null;
/*    */     } catch (Exception e) {
/* 28 */       throw new RuntimeException(e);
/*    */     }
/*    */   }
/*    */ 
/*    */   public String encrypt(String message)
/*    */   {
/* 34 */     if (message == null)
/* 35 */       return null;
/* 36 */     BASE64Encoder base64en = new BASE64Encoder();
/*    */     try {
/* 38 */       byte[] strBytes = message.getBytes("UTF8");
/* 39 */       Cipher cipher = Cipher.getInstance("DES");
/* 40 */       cipher.init(1, this.key);
/* 41 */       byte[] encryptStrBytes = cipher.doFinal(strBytes);
/* 42 */       return base64en.encode(encryptStrBytes); } catch (Exception e) {
/*    */     }
/* 44 */     throw new RuntimeException(e);
/*    */   }
/*    */ 
/*    */   public String decrypt(String encryptedMessage)
/*    */   {
/* 50 */     if (encryptedMessage == null)
/* 51 */       return null;
/* 52 */     BASE64Decoder base64De = new BASE64Decoder();
/*    */     try {
/* 54 */       byte[] strBytes = base64De.decodeBuffer(encryptedMessage);
/* 55 */       Cipher cipher = Cipher.getInstance("DES");
/* 56 */       cipher.init(2, this.key);
/* 57 */       byte[] decryptStrBytes = cipher.doFinal(strBytes);
/* 58 */       return new String(decryptStrBytes, "UTF8");
/*    */     } catch (Exception e) {
/* 60 */       e.printStackTrace();
/*    */     }
/* 62 */     return encryptedMessage;
/*    */   }
/*    */ }

/* Location:           C:\java代码反编译器\core-1.1.4.jar
 * Qualified Name:     com.eloancn.framework.cipher.TextDESEncryptor
 * JD-Core Version:    0.6.0
 */